package com.ruicar.afs.cloud.apply.contract.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruicar.afs.cloud.apply.common.entity.ApplyCustBaseInfo;
import com.ruicar.afs.cloud.apply.common.entity.ApplyOprRecord;
import com.ruicar.afs.cloud.apply.common.entity.ApplyOrderInfo;
import com.ruicar.afs.cloud.apply.contract.vo.ContractEntryCollectVO;
import com.ruicar.afs.cloud.apply.pre.loan.service.ApplyCustBaseInfoService;
import com.ruicar.afs.cloud.apply.pre.loan.service.ApplyOprRecordService;
import com.ruicar.afs.cloud.apply.pre.loan.service.ApplyOrderInfoService;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author: Lee
 * @date: 2020/5/11 19:39
 * @description: 特殊业务申请
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/special")
public class SpecialBusinessController {

    private ApplyCustBaseInfoService applyCustBaseInfoService;
    private ApplyOrderInfoService applyOrderInfoService;
    private ApplyOprRecordService applyOprRecordService;

    @PostMapping("/page/info")
    @ApiOperation(value = "获取特殊业务页面数据")
    public IResponse contractEntryInfo(@RequestParam("applyNo") String applyNo) {
        ContractEntryCollectVO vo = new ContractEntryCollectVO();
        List<ApplyCustBaseInfo> custBaseInfos = applyCustBaseInfoService.list(Wrappers.<ApplyCustBaseInfo>query().lambda()
                .eq(ApplyCustBaseInfo::getApplyNo, applyNo).eq(ApplyCustBaseInfo::getCustRole, "01"));
        ApplyOrderInfo orderInfo = applyOrderInfoService.getOne(Wrappers.<ApplyOrderInfo>query().lambda()
                .eq(ApplyOrderInfo::getApplyNo, applyNo));

        vo.setOrderInfo(orderInfo != null ? orderInfo : new ApplyOrderInfo());
        vo.setCustBaseInfos(custBaseInfos);
        return new IResponse<ContractEntryCollectVO>().setData(vo);
    }

    @PostMapping("/sumitSpecial")
    @ApiOperation(value = "提交特殊业务申请")
    public IResponse sumitSpecial(@RequestBody ApplyOprRecord special) {
        boolean result;
        if (special.getId() == null) {
            result = applyOprRecordService.save(special);
        } else {
            result = applyOprRecordService.updateById(special);
        }
        if (result) {
            return new IResponse().setMsg("提交成功");
        } else {
            return new IResponse().setCode("9999").setMsg("提交失败");
        }
    }

    @PostMapping("/getSpecialInfo")
    @ApiOperation(value = "获取特殊业务申请信息")
    public IResponse getSpecialInfo(@RequestBody ApplyOprRecord special) {
        ApplyOprRecord applyOprRecord = applyOprRecordService.getOne(Wrappers.<ApplyOprRecord>query().lambda().eq(ApplyOprRecord::getApplyNo, special.getApplyNo()));
        return IResponse.success(applyOprRecord);
    }
}
